﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.JShop.Web.Controls;
using CNVP.Framework;
using CNVP.Framework.Helper;
using System.Data;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin.CommonWindows
{
    public partial class ProductAdd_Select_Window : AdminPage
    {
        BLL.CNVP_Standard bll = new BLL.CNVP_Standard();
        public int rplistCount = 0;
        int PageIndex = 1;
        int ProductTypeID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ProductTypeID"]) && Public.IsNumber(Request.QueryString["ProductTypeID"]))
            {
                ProductTypeID = Convert.ToInt32(Request.QueryString["ProductTypeID"]);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!IsPostBack)
            {
                StartLoad(PageIndex);
               
            }
            btUp.Attributes.Add("onclick", "if(!ifCheck()){alert('请选择规格！');return false;};");
        }


        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            string StandardID_Str = "";
            var ProTypeRefStandard_List = new BLL.CNVP_ProTypeRefStandard().GetAll_CNVP_ProTypeRefStandard(ProductTypeID);
            for (int k = 0; k < ProTypeRefStandard_List.Count; k++)
            {
                StandardID_Str += ProTypeRefStandard_List[k].StandardID + ",";
            }
            if (StandardID_Str != "")
            {
                StandardID_Str = StandardID_Str.Substring(0, StandardID_Str.Length - 1);
            }
            SQLConditionInfo[] st = new SQLConditionInfo[1];
            st[0] = new SQLConditionInfo("@StandardID_Str", StandardID_Str);
            int i, j;
            DataTable dt = null;
            dt = bll.GetPage(this.GetType().Name, PageIndex, 10, out i, out j, st);
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Label lbStandardName = (Label)e.Item.FindControl("lbStandardName");
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            if (e.CommandName == "Select")
            {
                BasePage.CloseWindowReturnValues(id.ToString() + "*" + lbStandardName.Text);
            }
        }

        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //方法一
            if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
            {
                CheckBox cbCheck = (CheckBox)e.Item.FindControl("cbCheck");
                string Standard = Request.QueryString["Standard"].ToString();
                if (Standard != "")
                {
                    string[] proDid = Standard.Split(Convert.ToChar(","));
                    for (int i = 0; i < proDid.Length; i++)
                    {
                        if (cbCheck.ToolTip == proDid[i])
                        {
                            cbCheck.Checked = true;
                        }
                    }
                }
            }
        }
        string strCode = "";
        string strName = "";
        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.rpList.Items.Count > 0)
                {
                    for (int i = 0; i < this.rpList.Items.Count; i++)
                    {
                        CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                        Label lbStandardName = (Label)this.rpList.Items[i].FindControl("lbStandardName");
                        if (cbCheck.Checked == true)
                        {
                            strCode += cbCheck.ToolTip + ",";
                            strName += lbStandardName.Text + ",";
                        }
                    }
                    strCode = strCode.Substring(0, strCode.Length - 1);
                    strName = strName.Substring(0, strName.Length - 1);
                    BasePage.CloseWindowReturnValues(strCode + "*" + strName);
                }
            }
            catch
            { BasePage.CloseWindowReturnValues(strCode + "*" + strName); }
        }
    }
}